package com.bawei.rk_01.car

import androidx.lifecycle.viewModelScope
import com.bawei.base.utils.SingLiveData
import com.bawei.base.utils.bean.CarEntity
import com.bawei.base.utils.bean.GoodEnttiy
import com.bawei.base.utils.bean.OrderEntity
import com.bawei.base.utils.bean.Res
import com.bawei.base.utils.mvvm.BaseViewModel
import com.bawei.base.utils.param.Poeder
import kotlinx.coroutines.launch
import retrofit2.http.Query

class CarViewModel : BaseViewModel() {

    val carsuccess = SingLiveData<Res<CarEntity>>()
    val carfail = SingLiveData<String>()
    fun car(isValid:String, limit:Int, page:Int){
        viewModelScope.launch {
            try {
                carsuccess.postValue(apiService.getCat(isValid, limit, page))
            }catch (e:Exception){
                carfail.postValue(e.message.toString())
            }
        }
    }

    //修改购物车
    val updatecarsuccess = SingLiveData<Res<String?>>()
    val updatecarfail = SingLiveData<String>()
    fun updatecar(id:Int,number:Int){
        viewModelScope.launch {
            try {
                updatecarsuccess.postValue(apiService.updateCar(id, number))
            }catch (e:Exception){
                updatecarfail.postValue(e.message.toString())
            }
        }
    }

    //删除购物车
    val delcarsuccess = SingLiveData<Res<String?>>()
    val delcarfail = SingLiveData<String>()
    fun delcar(ids:Array<Int>){
        viewModelScope.launch {
            try {
                delcarsuccess.postValue(apiService.delcar(ids))
            }catch (e:Exception){
                delcarfail.postValue(e.message.toString())
            }
        }
    }

    //预下单
    val ordersuccess = SingLiveData<Res<OrderEntity>>()
    val orderfail = SingLiveData<String>()
    fun order(poeder: Poeder){
        viewModelScope.launch {
            try {
                ordersuccess.postValue(apiService.order(poeder))
            }catch (e:Exception){
                orderfail.postValue(e.message.toString())
            }
        }
    }



}